Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    houseInfo: '', //选择报修的房屋信息
    repairItemName: '',
    appointment: '',

    houseId: '', //已审核通过的房屋id
    repairItemId: '', //维修项目id
    mobile: '',
    appointment: '', //预约日期
    description: '', //问题描述


  },
  onLoad({
    id
  }) {
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      wx.setNavigationBarTitle({
        title: '修改报修信息',
      })
      this.getRepairDetail(id)
    }
  },

  // 获取保修信息
  async getRepairDetail(id) {
    if (!id) return
    const {
      code,
      data: repairDetail
    } = await wx.http.get('/repair/' + id)
    // 校验接口调用结果
    if (code !== 10000) return wx.utils.toast('获取报修详情失败')
    this.setData({
      ...repairDetail
    })
  },
  // 查询房屋列表
  async getHouseList() {
    const {
      code,
      data: houseList
    } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast('获取房屋失败')
    this.setData({
      houseList
    })
  },
  // 查询维修项目列表
  async getRepairItem() {
    const {
      code,
      data: repairItem
    } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast('获取维修项目失败')
    this.setData({
      repairItem
    })
  },
  // 选择维修的房屋
  selectHouse(e) {
    const {
      name: houseInfo,
      id: houseId
    } = e.detail
    this.setData({
      houseInfo,
      houseId
    })
  },
  // 选择维修的项目
  selectRepair(e) {
    const {
      name: repairItemName,
      id: repairItemId
    } = e.detail
    this.setData({
      repairItemName,
      repairItemId
    })
  },
  // 选择维修的时间
  selectAppointment(e) {
    this.setData({
      appointment: wx.utils.formatDate(e.detail),
      dateLayerVisible: false
    })
  },

  // 选择图片文件并上传
  async onAfterRead(e) {
    const res = await wx.http.upload('/upload', {
      name: 'file',
      filePath: e.detail.file.tempFilePath,
      formData: {}
    })
    // 更新数据
    if (res.code !== 10000) return wx.utils.toast('上传图片失败')
    const {
      attachment
    } = this.data
    attachment.push(res.data)
    this.setData({
      attachment
    })
  },

  // 删除图片
  onDelete(e) {
    console.log(e);
    // const {
    //   attachment
    // } = this.data
    // attachment.splice(e.detail.index, 1)

    this.data.attachment.splice(e.detail.index, 1)
    this.setData({
      attachment: this.data.attachment
    })
  },

  // 提交报修
  async submit() {
    const {
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
      id
    } = this.data
    // 数据检验
    if (!wx.verify.verifyHouse(houseId)) return
    if (!wx.verify.verifyRepair(repairItemId)) return
    if (!wx.verify.verifyMobile(mobile)) return
    if (!wx.verify.verifyDate(appointment)) return
    if (!wx.verify.verifyDescription(description)) return
    // 调用接口
    const {
      code
    } = await wx.http.post('/repair', {
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
      id
    })
    if (code !== 10000) return wx.utils.toast('提交报修失败')
    // 跳转页面
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },


  // --------------打开/关闭报修房屋弹层的显示/隐藏-------------------------
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },

  // --------------打开/关闭维修项目弹层的显示/隐藏-------------------------

  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  // --------------打开/关闭预约日期弹层的显示/隐藏-------------------------

  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})